package cn.itcast.shiro;

import cn.itcast.service.system.UserService;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;

public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {

    @Autowired
    private UserService userService;
    // 密码比较器(覆盖密码比较的方法)

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {

                //1、获取页面的密码
                UsernamePasswordToken unp = (UsernamePasswordToken) token;
                String password = new String(unp.getPassword());//111111
                String email = unp.getUsername();
                //2、将页面的密码进行加密（密码+邮箱+盐+加盐次数 ）
                String Md5password = new Md5Hash(password, email, 1).toString();
                System.out.println("页面加密后的密文 = " + Md5password);
                //3、获取安全数据的密码
                String dataPassword = (String) info.getCredentials();//加密后的
                System.out.println("安全数据的密码 " + dataPassword);
                //4、返回对比结果
                System.out.println("对比结果为：" + dataPassword.equals(Md5password));

                return dataPassword.equals(Md5password);



    }


}
